<template>
  <div class="tags" v-show="showTags">
    <ul>
      <li v-for="(tag,index) in tagsList" :key="index" class="tags-li" :class="{'active': $route.fullPath == tag.path}">
        <router-link :to="tag.path" class="tags-li-title">
          <span>{{tag.title}}</span>
        </router-link>
        <span class="tags-li-icon" @click="closeTag(index)"><i class="el-icon-close"></i></span>
      </li>
    </ul>
    <div class="tags-close-box">
      <el-dropdown @command="handleTags">
        <el-button type="primary" size="mini">
          标签选项<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="other">关闭其他</el-dropdown-item>
          <el-dropdown-item command="all">关闭所有</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>
<script>
import bus from './bus'
export default {
  data () {
    return {
      tagsList: []
    }
  },
  methods: {
    setTags (route) {
      // 判断新跳转的路由在页签列表中是否存在
      let exit = this.tagsList.some(item => {
        return item.path === route.fullPath
      })
      // 如果已经存在则不需任何操作
      if (exit) {
        return
      }
      // 如果不存在则将新的页签插入到页签列表tagsList
      this.tagsList.push({
        title: route.meta.title,
        path: route.fullPath,
        name: route.name
      })
      // this.showTags = true
      bus.$emit('tags', this.tagsList)
    },
    closeTag (index) {
      const delItem = this.tagsList.splice(index, 1)[0]// 删除项
      // 删除后判断是否还有剩余项 ，即判断删除当前页签后 下一个被选中的页签是那个？如果没有剩余则直接跳转到系统首页
      const item = this.tagsList[index] ? this.tagsList[index] : this.tagsList[index - 1]
      if (item) {
        // 如果删除   当前页签后还有剩余页签，判断删除的页签是否是当前路径的页签，是则完成跳转，否则不跳转
        delItem.path === this.$route.fullPath && this.$router.push(item.path)
      } else {
        // 如果删除的是最后一个页签，且是系统首页页签，则隐藏页签列(直接用计算属性根据tagsList的长度来控制是否隐藏和显示页签列)
        // delItem.path === '/dashboard' ? this.showTags = false : this.$router.push('/')
        this.$router.push('/')
      }
    },
    handleTags (command) {
      if (command === 'all') {
        this.closeAll()
      } else if (command === 'other') {
        this.closeOther()
      }
    },
    closeAll () {
      this.tagsList = []
      this.$router.push('/')
    },
    closeOther () {
      const arr = this.tagsList.filter(item => {
        return item.path === this.$route.fullPath
      })
      this.tagsList = arr
    }
  },
  watch: {
    $route (newValue, oldValue) {
      this.setTags(newValue)
    }
  },
  computed: {
    showTags () {
      return this.tagsList.length > 0
    }
  },
  created () {
    this.setTags(this.$route)
  }
}
</script>

<style scoped>
.tags {
  position: relative;
  height: 30px;
  overflow: hidden;
  background: #fff;
  padding-right: 120px;
  -webkit-box-shadow: 0 5px 10px #ddd;
  box-shadow: 0 5px 10px #ddd;
}
.tags-li {
  float: left;
  margin: 3px 5px 2px 3px;
  border-radius: 3px;
  font-size: 12px;
  overflow: hidden;
  cursor: pointer;
  height: 23px;
  line-height: 23px;
  border: 1px solid #e9eaec;
  background: #fff;
  padding: 0 5px 0 12px;
  vertical-align: middle;
  color: #666;
  -webkit-transition: all 0.3s ease-in;
  -moz-transition: all 0.3s ease-in;
  transition: all 0.3s ease-in;
}
.tags-li.active {
  color: #fff;
  border: 1px solid #409eff;
  background-color: #409eff;
}
.tags-li-title {
  float: left;
  max-width: 80px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  margin-right: 5px;
  color: #666;
}

.tags-li.active .tags-li-title {
  color: #fff;
}
.tags-close-box {
  position: absolute;
  right: 0;
  top: 0;
  box-sizing: border-box;
  padding-top: 1px;
  text-align: center;
  width: 110px;
  height: 30px;
  background: #fff;
  box-shadow: -3px 0 15px 3px rgba(0, 0, 0, 0.1);
  z-index: 10;
}
</style>
